var replytoUserId="";
var callname="";
//回答的ID
var reganid=/(aid=)([a-z0-9]*)/;
var anid="";
var isAjax=false;//接口调用状态
if(/(aid=)([a-z0-9]*)/.test(location.href)){
    anid=urlfrom.match(reganid).splice("=")[2].match(/[0-9]+/)[0];
}
var fromeurl=document.referrer;
var ajaxcomanswerurl="https://www.pulu.com/webapi/api/PuluAnswerComment/AddPuluAnswerComment_q";//默认为问题
if(/shareexppage/.test(fromeurl)){
    ajaxcomanswerurl="https://www.pulu.com/webapi/api/PuluAnswerComment/AddPuluAnswerComment_e";
}
if(/reply=/.test(location.href)){
    replytoUserId=(location.href).match(/reply=[0-9]+/)[0].split("=")[1];
}
if(/replyname=/.test(location.href)){
    callname=(location.href).match(/replyname=\S+/)[0].split("=")[1];
    if(/\%/.test(callname)){
        callname=decodeURIComponent(callname);
    }
    $("#commentinput").attr('placeholder','回复'+callname+"：");
}
//正常评论
function normalcomment(){
    if(document.getElementById("commentinput").value){
        btnisclick="yes";
        isAjax=true;
        console.log(anid);
        var oldword=document.getElementById("commentinput").value;
        $.ajax({
            type:"post",
            datatype:"json",
            url:ajaxcomanswerurl,
            data:{"id":"","answerid":anid,"content":document.getElementById("commentinput").value,"userId":id,"replytoCommentId":replytoUserId},
            success:function(data){
                $("#commentinput").val("");
                alert("评论成功");
                $.cookie("reloadPage","reloadPage");
                //浏览器 返回未刷新问题
                var comf=document.referrer;
                if(!/fromuser/.test(location.href)){comf+="#fromuser="+ $.cookie("MYID")}
                isAjax=false;
                location.href=comf;

            }
        });
    }
};
//新闻附带转发评论
function specialcomment(){
    if(document.getElementById("commentinput").value){
        btnisclick="yes";
        isAjax=true;
        var oldword=document.getElementById("commentinput").value;
        var puluanswer={};
        puluanswer.questionId=anid;
        puluanswer.content=document.getElementById("commentinput").value;
        puluanswer.answerUserId=id;
        $.ajax({
            type:"post",
            datatype:"json",
            url:"https://www.pulu.com/webapi/api/PuluAnswer/AddNewsAnswerFromout",
            data:{"puluanswer":puluanswer},
            success:function(data){
                console.log(data);
                $("#commentinput").val("");
                alert("评论成功");
                isAjax=false;
                location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+anid+"#fromuser="+id+"#newsShareId="+data.Message;
            }
        });
    }
};
//新闻直接评论 列表页功能
function newsnormalcomment(){
    if(document.getElementById("commentinput").value&&!/^\s+$/.test(document.getElementById("commentinput").value)){
        var newsshareId=(location.href).match(/shareNewsId=[0-9]+/)[0].match(/[0-9]+/)[0];
        var replyuserid="";
        if(/replyuser=/.test(location.href)){
            replyuserid=(location.href).match(/replyuser=[a-z0-9]+/)[0].split("=")[1];
        }
        var imgs=[];
        var answer={};
        answer.questionId=anid;
        answer.answerUserId=id;
        answer.atype=1;  //评论新闻为1
        answer.isShare=1;
        answer.shareId=newsshareId;
        answer.up=0;
        answer.down=0;
        answer.replyto = replyuserid;
        answer.replytoNick = callname;
        answer.content=document.getElementById("commentinput").value;
        isAjax=true;
        $.ajax({
            type:"post",
            datatype:"json",
            url:replyurl,
            data:{"puluanswer":answer,"imgs":imgs},
            success:function(data){
                $("#commentinput").val("");
                alert("评论成功");
                isAjax=false;
                location.href="subscribe.html";
                //location.reload();

                //location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+anid+"#fromuser="+id+"#newsShareId="+newsshareId;
            }
        });
    }
}
//经验评论
function expcomment(){
    if(document.getElementById("commentinput").value){
        var answer={};
        answer.questionId=anid;
        answer.content=document.getElementById("commentinput").value;
        answer.answerUserId=id;
        answer.atype=3;
        answer.up=0;
        answer.down=0;
        answer.starScore="";
        var labelids;
        isAjax=true;
        $.ajax({
            type:"post",
            datatype:"json",
            url:"https://www.pulu.com/webapi/api/PuluAnswer/AddAnswer_e",
            data:{"puluanswer":answer,"imgs":"","labelids":labelids},
            success:function(data){
                $("#commentinput").val("");
                alert("评论成功!");
                $.cookie("reloadPage","reloadPage");
                //浏览器 返回未刷新问题
                var comf=document.referrer;
                isAjax=false;
                if(!/fromuser/.test(location.href)){comf+="#fromuser="+ $.cookie("MYID")}
                location.href=comf;
            }
        })
    }
}
document.getElementById("sendcommentbtn").onclick=function(){
    if(!isAjax){//当前没有接口调用
        if(/newsdetail/.test(fromeurl)||/subscribe.html/.test(fromeurl)){
            //判断来源页面 阅读圈 列表页
            //直接评价
            if(/shareNewsId/.test(location.href)){newsnormalcomment();}
            //转发 评价
            else{specialcomment();}
        }else{
            if(/fromexp/.test(location.href)){expcomment();}
            else{normalcomment();}
        }
    }
};
$("#cancelbtn").click(function(){
    history.go(-1);
});
var cancelbtn=document.getElementById("cancelbtn");//取消当前恢复按钮
////键盘事件
//document.onkeydown=function(event){
//    var e = event || window.event || arguments.callee.caller.arguments[0];
//    if(e && e.keyCode==13){ // enter 键
//        if(/newsdetail/.test(fromeurl)){
//            //判断来源页面 阅读圈 列表页
//            //直接评价
//            if(/shareNewsId/.test(location.href)){newsnormalcomment();}
//            //转发 评价
//            else{specialcomment();}
//        }else{
//            if(/fromexp/.test(location.href)){expcomment();}
//            else{normalcomment();}
//        }
//    }
//};

//监听textarea 状态
$('#commentinput').bind('input propertychange', function(a) {
    var a= a.target;
    if(document.getElementById("commentinput").value){
        //自适应高度
        var wid=document.getElementById("commentinput").style.width;
        a.cols=parseInt((wid-10)/15);
        var col=a.cols;
        var val=a.value.length;
        var rownum=1;
        if(val>=col){
            rownum=Math.ceil(val/col);
            //console.log("rownum",rownum);
            if(rownum>=3){
                a.style.overflowY="auto";
                a.rows=3;
            }else{
                a.rows=rownum;
            }
        }
        document.getElementById("sendcommentbtn").className="cmnt_smt";
    }else{
        document.getElementById("sendcommentbtn").className="cmnt_smt none_cmnt";
    }
});